<?php

namespace App\Services;

use App\Constants\ContentReCommendConstant;
use App\Http\Resources\ArticleResource;
use App\Http\Resources\Course\CourseListResource;
use App\Models\Article;
use App\Models\Course;
use App\Models\JumpTag;
use App\Models\MiniProgramMainTab;
use App\Models\MiniProgramSpecial;
use Illuminate\Support\Str;

class SpecialService
{
    public function getSpecialData(string $miniType, string $type = 'index')
    {
        return MiniProgramSpecial::where([
            'mini_type' => $miniType,
            'is_show'   => 1,
            'type'      => $type
        ])
            ->orderByDesc('sort')
            ->orderByDesc('id')
            ->get()
            ->each(function ($item) {
                $item->data = $this->getData($item);
            });
    }

    protected function getData(MiniProgramSpecial $special)
    {
        $method = 'get' . Str::camel($special->module_slug) . 'Data';

        if (method_exists($this, $method)) {
            return $this->{$method}($special);
        }

        return null;
    }

    // 顶部导航模块
    protected function getTopNavModuleData(MiniProgramSpecial $special)
    {
        return MiniProgramMainTab::with(['jump' => function ($q) {
            return $q->where('is_show', 1);
        }])->where([
            'mini_type' => $special->mini_type,
            'type'      => MiniProgramMainTab::TYPE_BY_TOP,
            'is_show'   => 1,
        ])->orderByDesc('sort')->get();
    }

    // 中部导航模块
    protected function getNavModuleData(MiniProgramSpecial $special)
    {
        return MiniProgramMainTab::with(['jump' => function ($q) {
            return $q->where('is_show', 1);
        }])->where([
            'mini_type' => $special->mini_type,
            'type'      => MiniProgramMainTab::TYPE_BY_CENTRAL,
            'is_show'   => 1,
        ])->orderByDesc('sort')->get();
    }

    // 轮播图模块
    protected function getRotationChatModuleData(MiniProgramSpecial $special)
    {
        $banners = getMiniSettings($special->mini_type, 'special', 'rotation_chat');

        if (!$banners) return [];

        $banners = array_values(json_decode($banners, true));

        $jumpIds = array_unique(array_filter(array_column($banners, 'jump_id')));
        if ($jumpIds) {
            $jumpTag = JumpTag::whereIn('id', $jumpIds)->where('is_show', 1)->get();
        }

        foreach ($banners as &$item) {
            $item['jump'] = ($item['jump_id'] && isset($jumpTag)) ? $jumpTag->where('id', $item['jump_id'])->first() : null;
        }

        return $banners;
    }

    protected function getSingleImgModuleData(MiniProgramSpecial $special)
    {
        $data = getMiniSettings($special->mini_type, 'special', 'single_img');

        if (!$data) return null;

        $data = json_decode($data, true) ?: [];

        if (!$data) return null;

        $data['jump'] = (isset($data['jump_id']) && $data['jump_id']) ? JumpTag::where('id', $data['jump_id'])->where('is_show', 1)->first() : null;

        return $data;
    }

    // 穴位与病症模块
    protected function getXwDiseaseModuleData(MiniProgramSpecial $special)
    {
        return null;
    }

    // 新闻两格模块
    protected function getTopArticleModuleData(MiniProgramSpecial $special)
    {
        $list = Article::with(['publisher', 'comments', 'tag'])
            ->where([
                'mini_type' => $special->mini_type,
                'type'      => 2,
            ])
            ->where('is_top', 1)
            ->orderByDesc('id')
            ->limit(2)
            ->get();

        return ArticleResource::collection($list);
    }

    // 新闻单列模块
    protected function getTjArticleModuleData(MiniProgramSpecial $special)
    {
        $settings = getMiniSettings($special->mini_type, ContentReCommendConstant::GROUP);
        $day = $settings[ContentReCommendConstant::ARTICLE_TJ_DAY] ?? 30;

        $list = Article::with(['publisher', 'comments', 'tag'])
            ->where([
                'mini_type' => $special->mini_type,
                'type'      => 2
            ])
            ->whereDate('created_at', '>=', now()->subDays($day)->format('Y-m-d'))
            ->where('is_hot', 1)
            ->orderByDesc('id')
            ->limit(3)
            ->get();

        if (!$list->count()) {
            $list = Article::with(['publisher', 'comments', 'tag'])
                ->where([
                    'mini_type' => $special->mini_type,
                    'type'      => 2
                ])
                ->orderByDesc('id')
                ->limit(3)
                ->get();
        }

        return ArticleResource::collection($list);
    }

    // 快讯单列模块
    protected function getTjHotArticleModuleData(MiniProgramSpecial $special)
    {
        $settings = getMiniSettings($special->mini_type, ContentReCommendConstant::GROUP);
        $day = $settings[ContentReCommendConstant::HOT_NEWS_TJ_DAY] ?? 30;

        $list = Article::with(['publisher', 'comments', 'hot_news_tag'])
            ->where([
                'mini_type' => $special->mini_type,
                'type'      => 1,
            ])
            ->whereDate('created_at', '>=', now()->subDays($day)->format('Y-m-d'))
            ->orderByDesc('is_top')
            ->orderByDesc('is_hot')
            ->orderByDesc('id')
            ->limit(6)
            ->get();

        if (!$list->count()) {
            $list = Article::with(['publisher', 'comments', 'hot_news_tag'])
                ->where([
                    'mini_type' => $special->mini_type,
                    'type'      => 1,
                ])
                ->orderByDesc('id')
                ->limit(6)
                ->get();
        }

        return ArticleResource::collection($list);
    }

    public function getTjCourseModuleData(MiniProgramSpecial $special)
    {
        $list = app(CourService::class)->list($special->mini_type, 0, 3);

        return CourseListResource::collection($list);
    }
}
